package com.qf.common;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName CustomMultipartFile
 * @Date: 2025/5/5 18:14
 * @Description: 描述
 */


public class CustomMultipartFile implements MultipartFile {
    private final File file;
    private final String originalFilename;
    private final String contentType;

    /**无参构造方法**/
    public CustomMultipartFile() {
        this.file = null;
        this.originalFilename = null;
        this.contentType = null;
    }

    /**
     * 构造方法
     * @param file 原始二进制文件
     * @param originalFilename 原始文件名
     * @param contentType 文件类型
     */
    public CustomMultipartFile(File file, String originalFilename, String contentType) {
        // 原始二进制文件
        this.file = file;
        // 原始文件名
        this.originalFilename = originalFilename;
        // 文件类型
        this.contentType = contentType;
    }

    @Override
    public String getName() {
        return this.originalFilename;
    }

    @Override
    public String getOriginalFilename() {
        return this.originalFilename;
    }

    @Override
    public String getContentType() {
        return this.contentType;
    }

    @Override
    public boolean isEmpty() {
        return this.file.length() == 0;
    }

    @Override
    public long getSize() {
        return this.file.length();
    }

    @Override
    public byte[] getBytes() throws IOException {
        return java.nio.file.Files.readAllBytes(this.file.toPath());
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new java.io.FileInputStream(this.file);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        java.nio.file.Files.move(this.file.toPath(), dest.toPath());
    }

    public static MultipartFile urlToFile(String fileUrl) throws Exception {
        URL url = new URL(fileUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("get");
        httpConn.connect();

        int responseCode = httpConn.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new Exception("无法从URL下载文件");
        }

        // 从URL获取文件名
        String disposition = httpConn.getHeaderField("Content-Disposition");
        String contentType = httpConn.getContentType();
        String fileName = disposition != null ? disposition.substring(disposition.indexOf("filename=") + 9, disposition.length() - 1) : new File(fileUrl).getName();

        // 创建临时文件来存储下载的内容
        File tempFile = File.createTempFile(fileName, null);
        try (InputStream inputStream = httpConn.getInputStream();
             FileOutputStream outputStream = new FileOutputStream(tempFile)) {

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }

        // 返回自定义的MultipartFile对象
        return new CustomMultipartFile(tempFile, fileName, contentType);
    }
}
